bool fun(vector<int>& v1, vector<int>& v2) {
    if (v1[0] == v2[0]) {
        return v1[1] < v2[1];
    }
    return v1[0] < v2[0];
}
class Solution {
public:
    vector<int> maximumBeauty(vector<vector<int>>& items,
                              vector<int>& queries) {
        int n = items.size(), m = queries.size();
        sort(items.begin(), items.end(), fun);
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            cnt = items[i][1] = max(cnt,items[i][1]);
        }

        vector<int> ans(m);
        for (int i = 0; i < m; i++) {
            int j = queries[i];
            int left = 0, right = n - 1;
            while (left <= right) {
                int mid = (left + right) >> 1;
                if (items[mid][0] <= j) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
            if (left == 0) {
                ans[i] = 0;
            } else
                ans[i] = items[right][1];
        }
        return ans;
    }
};